/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package db.definitions;

/**
 *
 * @author abracadabuda
 */
public class ColumnDefinition extends AbstractDefinition {

    private String name;
    private String colName;
    private int position;
    private String type;
    private boolean pk;
    private boolean nullable;
    private String defaultValue;
    private String comment;
    private TableDefinition table;

    public ColumnDefinition(TableDefinition table) {
        this.table = table;
    }

    public ColumnDefinition() {

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getColName() {
        return colName;
    }

    public void setColName(String colName) {
        this.colName = colName;
    }

    public int getPosition() {
        return position;
    }

    public void setPosition(int position) {
        this.position = position;
    }

    public String getType() {
        return type;
    }

    /**
     * sets the type of column
     * @param type (from XML file)
     */
    public void setType(String type) {
        if (type.toString().equals("timestamptz")) { //hack :)
            type = "timestamp with time zone";
        }
        if (type.toString().equals("varchar(2147483647)")) { //hack :)
            type = "varchar";
        }
        if(type.toString().equals("tag")) {
            type = "\"" + prefix + "\".tag";
        }
        //TODO: prerobit to na nejake pekne asociativne pole :)
        this.type = type;
    }

    public String getDefaultValue() {
        return defaultValue;
    }

    public void setDefaultValue(String defaultValue) {
        if (defaultValue.startsWith("nextval")) { //this is generated by DB
            defaultValue = "";
        }
        this.defaultValue = defaultValue;
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public boolean isNullable() {
        return nullable;
    }

    public void setNullable(boolean nullable) {
        this.nullable = nullable;
    }

    public boolean isPk() {
        return pk;
    }

    public void setPk(boolean pk) {
        this.pk = pk;
    }

    /**
     * prints the dump of column (for testing)
     */
    
    public void getDump() {
        System.out.println("\t\tColumn:");
        System.out.println("\t\t\tName: " + name);
        System.out.println("\t\t\tCol-name: " + colName);
        System.out.println("\t\t\tPosition: " + position);
        System.out.println("\t\t\tType: " + type);
        System.out.println("\t\t\tIs PK: " + pk);
        System.out.println("\t\t\tIs null: " + nullable);
        System.out.println("\t\tDefault: " + defaultValue);
        System.out.println("\t\tComment: " + comment);
    }

    /**
     * gets the SQL column code used be CREATE TABLE
     * @return SQL code
     */
    
    public String getColumnForCreateTableSQL() {
        String sql = "\"" + colName + "\" " + type + " ";
        sql += nullable ? "NULL" : "NOT NULL";
        sql += defaultValue.equals("") ? "" : " DEFAULT " + defaultValue;

        return sql;
    }

    /**
     * gets the comment of current column
     * @return SQL code
     */
    
    public String getCommentSQL() {
        if (comment.equals("")) {
            return "";
        }
        String sql = "COMMENT ON COLUMN ";
        sql += "\"" + table.getSchema() + "\".";
        sql += "\"" + table.getTblName() + "\".\"" + colName + "\" ";
        sql += "IS '" + comment + "';\n";
        return sql;
    }

    /**
     * gets the table, the column belongs to
     * @return
     */
    public TableDefinition getTable() {
        return table;
    }

    /**
     * returns ""
     * @return ""
     */
    public String getSQL() {
        return "";
    }
}
